
#include "string.h"
#include "stdio.h"


//链表信息
struct ListNode {
    int val;
    struct ListNode *next;
};


//删除链表中重复的节点
static struct ListNode* removeDuplicateNodes(struct ListNode* head) {
    if (head == NULL) {
        return NULL;
    }
    struct ListNode *cur = head;

    while(cur != NULL) {

        struct ListNode *p= cur;
        while (p->next != NULL) {
            if (p->next->val == cur->val) {
                p->next = p->next->next;
            } else {
                p = p->next;
            }
        }
        cur = cur->next;
    }

    return head;
}


//反转链表
static struct ListNode* ReverseList(struct ListNode* head)
{
    if (head == NULL) {
        return NULL;
    }

    struct ListNode *cur = head;
    struct ListNode *pre = NULL;

    while (cur != NULL) {
        struct ListNode *temp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = temp;
    }

    return pre;
}

void add_item_after(struct ListNode* pre, struct ListNode* new)
{   
    if ((pre == NULL) || (new == NULL))
        return;

    new->next = pre->next;
    pre->next = new;
}


void printf_list(struct ListNode* head)
{
    struct ListNode *curr = head;

    while (curr != NULL)  {
        printf("List val=%d\r\n", curr->val);
        curr = curr->next;
    }
}

#define ITEM_LEN 6

static struct ListNode head;
struct ListNode new_list[ITEM_LEN] = {
    { 1, NULL},
    { 2, NULL},
    { 3, NULL},
    { 5, NULL},
    { 5, NULL},
    { 6, NULL},
};

void list_test(void)
{
    int i = 0;

    head.val  = 10;
    head.next = NULL;

    for (i = 0; i < ITEM_LEN; i++) {
        add_item_after(&head, &new_list[i]);
    }

    //1.反转链表
    struct ListNode *node = ReverseList(&head);
    printf("ReverseList:\r\n");
    printf_list(node);

    //2.删除重复元素
    printf("removeDuplicateNodes:\r\n");
    removeDuplicateNodes(node);
    printf_list(node);

}



